/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	using DataProvider;

	/// <summary>
	/// Summary description for ResLinkRateLink.
	/// </summary>
	public class ResLinkRateLink : CommunityServer.Controls.RatingControl
	{
		public ResLinkRateLink()
		{

		}


		public int PostID
		{
			get
			{
				return (int)ViewState["PostID"];
			}
			set
			{
				ViewState["PostID"] = value;
			}
		}

		public int SectionID
		{
			get
			{
				return (int)ViewState["SectionID"];
			}
			set
			{
				ViewState["SectionID"] = value;
			}
		}

		CSContext csContext = CSContext.Current;

		#region Public Properties

		private ResLinkLink _link = null;
		public ResLinkLink Post
		{
			get
			{
				if(_link == null)
				{
					_link = ResLinkDataProvider.Instance().GetLink(SectionID , PostID);
				}
				return _link;
			}
			set
			{
				_link = value;

				if (_link != null)
				{
					SectionID = _link.SectionID;
					PostID = _link.ParentID;
				}
			}
		}

		#endregion

		#region RatingControl Implementation

		private double _currentRating = -1;
		public override double CurrentRating
		{
			get
			{
				if (_currentRating == -1)
					_currentRating = this.Post.RatingAverage;

				return _currentRating;
			}
			set
			{
				_currentRating = value;
			}
		}

		private int _currentVotes = -1;
		public override int CurrentVotes
		{
			get
			{
				if (_currentVotes == -1)
					_currentVotes = Post.TotalRatings;

				return _currentVotes;
			}
			set
			{
				_currentVotes = value;
			}
		}

		private string _viewDetailsScript = String.Empty;
		public override string ViewDetailsScript
		{
			get
			{
				return _viewDetailsScript;
			}
			set
			{
				_viewDetailsScript = value;
			}
		}
		//I'm using this to control the page so that 
		//a user cant rate without adding a comment
		public override bool UserCanRate 
		{ 
			get
			{
				return false;
			}
		}

		public override void Rate(double value)
		{
			throw new Exception("Cant rate a resource link directly you must add a comment");
		}

		#endregion
	}
	
	public class ResLinkCommentRateLink : CommunityServer.Controls.RatingControl
	{
		private HtmlInputHidden _SelectedRating = null;

		protected override void CreateChildControls()
		{
			base.CreateChildControls ();

			if (_SelectedRating == null)
			{
				_SelectedRating = new HtmlInputHidden();
				_SelectedRating.ID = "RattingValue";
				this.Controls.Add(_SelectedRating);
			}
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			CreateChildControls();

			if (UserCanRate)
				Page.RegisterOnSubmitStatement(this.ClientID , "document.getElementById('" + _SelectedRating.ClientID +"').value = document.getElementById('" + this.ClientID + "_Value').value;");
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
		}

		public bool AjaxCall = false;
		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{		
			//Cludge to work with ajax callback
			if (AjaxCall)
			{
				StringBuilder script = new StringBuilder();

				if (!this.Page.IsClientScriptBlockRegistered(base.GetType().FullName))
				{
					script.Append(string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/ratingcontrol.js")));
				}

				script.Append("<script language=\"javascript\">var ");
				script.Append(this.ClientID);
				script.Append("=new RateControl('");
				script.Append(this.ClientID);
				script.Append("','");
				script.Append(this.ClientID);
				script.Append("','");
				script.Append(this.ClientID);
				script.Append("_Value', 5, '");
				script.Append(this.Context.Response.ApplyAppPathModifier(this.ImagesBaseUrl));
				script.Append("', false, '");
				script.Append(this.CssClass);
				script.Append("','");
				script.Append(this.ActiveCssClass);
				script.Append("','");
				script.Append(this.ReadOnlyCssClass);
				script.Append("',['");
				script.Append(ResourceManager.GetString("PostRating_Zero").Replace("'", "\'"));
				script.Append("','");
				script.Append(ResourceManager.GetString("PostRating_One").Replace("'", "\'"));
				script.Append("','");
				script.Append(ResourceManager.GetString("PostRating_Two").Replace("'", "\'"));
				script.Append("','");
				script.Append(ResourceManager.GetString("PostRating_Three").Replace("'", "\'"));
				script.Append("','");
				script.Append(ResourceManager.GetString("PostRating_Four").Replace("'", "\'"));
				script.Append("','");
				script.Append(ResourceManager.GetString("PostRating_Five").Replace("'", "\'"));
				script.Append("'],");
				script.Append((!this.IsReadOnly && this.UserCanRate) ? "false" : "true");
				script.Append(",");
				script.Append(this.IsReadOnly ? "false" : "true");
				script.Append(");</script>");

				writer.Write(script.ToString());
			}

			base.Render (writer);
		}

		public ResLinkCommentRateLink()
		{

		}

		private int _SectionID = -1;
		public int SectionID
		{
			get
			{
				return _SectionID;
			}
			set
			{
				_SectionID = value;
			}
		}

		private int _PostID = -1;
		public int PostID
		{
			get
			{
				return _PostID;
			}
			set
			{
				_PostID = value;
			}
		}


		private ResLinkComments comment = null;
		public ResLinkComments Comment 
		{
			get 
			{
				if (comment == null)
				{
					comment = new ResLinkComments();
					comment.PostID = PostID;
					comment.SectionID = SectionID;
				}

				return comment;
			}
			set 
			{
				comment = value;
			}
		}

		CSContext csContext = CSContext.Current;
		public event EventHandler RatingsChanged;

		#region RatingControl Implementation

		private double _currentRating = -1;
		public override double CurrentRating
		{
			get
			{
				if (_currentRating == -1 && this.Comment != null)
					_currentRating = this.Comment.CommentRating;

				return _currentRating;
			}
			set
			{
				_currentRating = value;
			}
		}

		public override int CurrentVotes
		{
			get
			{
				return 1;
			}
			set
			{
			}
		}

		private string _viewDetailsScript = String.Empty;
		public override string ViewDetailsScript
		{
			get
			{
				return _viewDetailsScript;
			}
			set
			{
				_viewDetailsScript = value;
			}
		}

		private bool _userCanRate = true;

		public bool EnableUserRating
		{
			set
			{
				_userCanRate = value;
			}
		}

		public int NewRating
		{
			get
			{
				if (_SelectedRating.Value == "")
					return -1;

				return System.Convert.ToInt32(_SelectedRating.Value);
			}
		}
		//I'm using this to control the page so that 
		//a user cant rate without adding a comment
		public override bool UserCanRate 
		{ 
			get
			{
				return _userCanRate;
			}
		}

		public override void Rate(double value)
		{
			Comment.TotalRatings =1;
			Comment.RatingSum = System.Convert.ToInt32(value);
			Comment.CommentRating = Comment.RatingSum ;

			OnRatingsChange(new EventArgs());
		}

		#endregion

		protected void OnRatingsChange(EventArgs e)
		{
			_SelectedRating.Value = Comment.CommentRating.ToString();

			if(RatingsChanged != null)
				RatingsChanged(this,e);
		}
	}
	
}
